var sendtohref=location.href.match(/sendquestion.html\?qtype=[0-9]/)[0];//改写 不同页面 不同情况
$.ajax({
    type:"GET",
    url:"GetSign.ashx?p="+sendtohref,
    success:function(data){
        timestampone=data.timestamp;
        nonceStrone=data.nonceStr;
        signatureone=data.signature;
        wx.config({
            debug: false,
            appId: 'wx658c34793ecfd9f9',
            timestamp:timestampone, //时间戳
            nonceStr:nonceStrone,  //随机字符串
            signature:signatureone,  //SHA1编码
            jsApiList: [
                // 所有要调用的 API 都要加到这个列表中
                "chooseImage","uploadImage","previewImage","downloadImage"
            ]
        });
    }
});
//展示大图片------------------------------------------------
var winscrolltop;
var imghash={};//保存服务器返回的图片地址 “images.localId[i]”：imgurl
var delimgid="";//要删除的图片的Id
//展示大图
function showbigimg(e){
    winscrolltop=$(window).scrollTop();
    //console.log(winscrolltop);
    $("#page_wrapper").css("display","none");
    $("#showimg").attr("src", e.dataset.img);//大图片的地址 和 Id 相同
    $("#digimgplace").css("display","block");
    var bigheight=$("#digimgplace").height();
    var imgheight=$("#showimg").height();
    if(bigheight>imgheight){
        $("#showimg").css("marginTop",(bigheight-imgheight)/2);
    }
    delimgid=e.dataset.img;//地址 和 Id 相同
}
//删除大图
$("#delthisimgbtn").click(function(e){
    imghash[delimgid]="";//清除对应ID 保存的服务器图片地址
    //页面中删掉图片
    var imgbox=document.getElementsByClassName("mainshowimgbox");
    for(var i= 0,r=imgbox.length;i<r;i++){
        if(imgbox[i].dataset.img==delimgid){
            //$imgbox=$(imgbox[i].dataset.img);
            $(".mainshowimgbox")[i].remove();
        }
    }
    $(".modifier.image").css("display","inlineBlock");
    $("#page_wrapper").css("display","block");
    $("#digimgplace").css("display","none");
});
var images = {
    localId: [],
    serverId: []
};
var sendlocalIds;
imgbtn.onclick=function(){
    //微信部分==========================================
    var imgnum=9;//选择图片个数
    if(document.getElementsByClassName("mainshowimgbox")&&document.getElementsByClassName("mainshowimgbox").length>0){
        imgnum=9-document.getElementsByClassName("mainshowimgbox").length;
    }
    wx.chooseImage({
        count: imgnum, // 默认9
        success: function(res) {
            images.localId = res.localIds;
            //alert('已选择 ' + res.localIds.length + ' 张图片');
            if (images.localId.length == 0) {
                alert('请先使用 chooseImage 接口选择图片');
                return;
            }else{
                var divheight=parseFloat(window.innerWidth)*0.21;
                for(var r=0;r<images.localId.length;r++){
                    $("<div class='mainshowimgbox' style='height:"+divheight+"px;overflow:hidden;' onclick='showbigimg(this)' data-img='"+images.localId[r]+"'><img src='"+images.localId[r]+"'></div>").insertBefore("#questionbody .modifier.image");
                }
            }
            var i = 0, length = images.localId.length;
            images.serverId = [];
            function upload(){
                wx.uploadImage({
                    localId: images.localId[i],
                    success: function(res) {
                        var locaimgurl=images.localId[i];
                        i++;
                        //alert('已上传：' + i + '/' + length);
                        images.serverId.push(res.serverId);
                        //alert(images.serverId);
                        //请求服务器下载图片
                        $.ajax({
                            type:"get",
                            url:wxgetimgurl+images.serverId,
                            success:function(img){
                                //alert(locaimgurl);
                                //alert(img);
                                //alert(img.replace("C:\\Images\/",""));
                                imghash[images.localId[i]]=img.replace("C:\\Images\/","");
                                //判断图片个数 9个隐藏按钮
                                if(document.getElementsByClassName("mainshowimgbox")&&document.getElementsByClassName("mainshowimgbox").length==9){
                                    $(".modifier.image").css("display","none");
                                }
                                //若果没上传好就继续
                                if (i < length) {
                                    upload();
                                }
                            }
                        });
                    },
                    fail: function(res) {
                        alert("图片上传失败了");
                        //alert(JSON.stringify(res));
                    }
                });
            }
            upload();
        }
    });
};
$("#addimgfilebtn").height(($(window).width()-30)*0.23);